package com.training.controller;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.training.common.Result;
import com.training.common.TrainingPage;
import com.training.common.model.Subject;
import com.training.common.model.User;
import com.training.service.SubjectService;
import com.training.service.SubjectServiceImp;

import java.util.*;

public class SubjectController extends Controller {

    @Inject(SubjectServiceImp.class)
    private SubjectService subjectService;

    public void index(){
        render("/views/subject/index.html");
    }

    public void getSubjectPage(){
        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");
        String name = getPara("name");
        Page<Subject> menuList = subjectService.getSubjectPage(pageNumber,pageSize,name);
        Result result = new Result();
        result.setData(new TrainingPage(menuList));
        renderJson(result);
    }

    public void showSubject(){
        if(getPara("id")==null){
            render("/views/subject/add.html");
        }else{
            String id = getPara("id");
            setAttr("id",id);
            render("/views/subject/edit.html");
        }

    }

    public void addSubject(){
        String name = getPara("name");
        Subject subject = new Subject();
        subject.setName(name);
        User user = getSessionAttr("trainingsession");
        subject.setUserId(user.getID());
        subject.setCreatetime(new Date());
        boolean boo = subject.save();
        Result result = new Result();
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("添加成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("添加失败！");
        }
        renderJson(result);
    }

    public void getSubject(){
        String id = getPara("id");
        Subject subject = Subject.dao.findById(id);
        Result result = new Result();
        result.setData(subject);
        renderJson(result);
    }

    public void editSubject(){
        String id = getPara("id");
        String name = getPara("name");
        Result result = new Result();
        Subject subject = Subject.dao.findById(id);
        subject.setName(name);
        boolean boo = subject.update();

        if (boo) {
            result.setErrorNo(0);
            result.setErrorInfo("更新成功！");
        } else {
            result.setErrorNo(500);
            result.setErrorInfo("更新失败！");
        }
        renderJson(result);
    }

    @Before(Tx.class)
    public void deleteSubject(){
        String ids = getPara("id");
        String[] idss = ids.split(",");
        Result result = new Result();
        boolean boo = false;
        for (String id : idss){
            int idi = Integer.parseInt(id);
            boo = Subject.dao.deleteById(idi);
        }
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("删除成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("删除失败！");
        }
        renderJson(result);
    }

    public void getSubjectList(){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        dataMap.put("code",0);
        dataMap.put("msg","success");
        List<Subject> subjectList = Subject.dao.getSubjectList();
        List<Map<String,Object>> data = new ArrayList<>();
        Map<String,Object> map = null;
        for(Subject subject : subjectList){
            map = new HashMap<String,Object>();
            map.put("name",subject.getName());
            map.put("value",subject.getId());
            data.add(map);
        }
        dataMap.put("data",data);
        renderJson(dataMap);
    }

    public void getTrainSubjectList(){
        String trainid = getPara("trainid");
        Map<String,Object> dataMap = new HashMap<String,Object>();
        dataMap.put("code",0);
        dataMap.put("msg","success");
        List<Subject> subjectList = Subject.dao.getTrainSubjectList(trainid.split(","));
        List<Map<String,Object>> data = new ArrayList<>();
        Map<String,Object> map = null;
        for(Subject subject : subjectList){
            map = new HashMap<String,Object>();
            map.put("name",subject.getName());
            map.put("value",subject.getId());
            data.add(map);
        }
        dataMap.put("data",data);
        renderJson(dataMap);
    }

}
